package com.lutao.util;

import com.lutao.util.Tencent.sign.TencentAISign;
import com.lutao.util.Tencent.sign.TencentAISignSort;
import com.lutao.util.Tencent.util.Base64Util;
import com.lutao.util.Tencent.util.FileUtil;
import com.lutao.util.Tencent.util.HttpsUtil4Tencent;
import com.lutao.util.Tencent.util.TencentAPI;

import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Master.Lu
 * @version V1.0
 * @name: YoutuUtil
 * @package com.lutao.util
 * @description TODO:
 * @date 2018-01-02 14:30
 */
public class YoutuUtil {
    public static Map<String, Object> faceMerge(byte[] file, String model) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            //时间戳
            String time_stamp = System.currentTimeMillis() / 1000 + "";
            //图片的base64编码数据
            String img64 = Base64Util.encode(file);
            //随机字符串
            String nonce_str = TencentAISign.getRandomString(10);
            Map<String, String> person_Id_body = new HashMap<String, String>();
            person_Id_body.put("app_id", String.valueOf(TencentAPI.APP_ID_AI));
            person_Id_body.put("time_stamp", time_stamp);
            person_Id_body.put("nonce_str", nonce_str);
            person_Id_body.put("image", img64);
            person_Id_body.put("model", model);
            String sign = TencentAISignSort.getSignature(person_Id_body);
            person_Id_body.put("sign", sign);
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/x-www-form-urlencoded");
            HttpResponse responseBD = HttpsUtil4Tencent.doPostTencentAI(TencentAPI.FACE_MERGE, headers, person_Id_body);
            String json = EntityUtils.toString(responseBD.getEntity());
            JSONObject object = JSONObject.fromObject(json);
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap = (Map) JSONObject.toBean(JSONObject.fromObject(object), Map.class);
            if ("0".equals(paramMap.get("ret").toString())) {
                paramMap = (Map) JSONObject.toBean(JSONObject.fromObject(paramMap.get("data")), Map.class);
                map.put("code", "success");
                map.put("mes", "人脸融合成功");
                map.put("data", paramMap);
            } else {
                map.put("code", "fail");
                map.put("mes", "人脸融合失败");
            }
        } catch (Exception e) {
            map.put("code", "fail");
            map.put("mes", "人脸融合异常");
            return map;
        }
        return map;
    }
    public static Map<String, Object> faceMerge(String file, String model) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            //时间戳
            String time_stamp = System.currentTimeMillis() / 1000 + "";
            //图片的二进制数组数据
            byte[] imageData = FileUtil.readFileByBytes(file);
            //图片的base64编码数据
            String img64 = Base64Util.encode(imageData);
            //随机字符串
            String nonce_str = TencentAISign.getRandomString(10);
            Map<String, String> person_Id_body = new HashMap<String, String>();
            person_Id_body.put("app_id", String.valueOf(TencentAPI.APP_ID_AI));
            person_Id_body.put("time_stamp", time_stamp);
            person_Id_body.put("nonce_str", nonce_str);
            person_Id_body.put("image", img64);
            person_Id_body.put("model", model);
            String sign = TencentAISignSort.getSignature(person_Id_body);
            person_Id_body.put("sign", sign);
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/x-www-form-urlencoded");
            HttpResponse responseBD = HttpsUtil4Tencent.doPostTencentAI(TencentAPI.FACE_MERGE, headers, person_Id_body);
            String json = EntityUtils.toString(responseBD.getEntity());
            JSONObject object = JSONObject.fromObject(json);
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap = (Map) JSONObject.toBean(JSONObject.fromObject(object), Map.class);
            if ("0".equals(paramMap.get("ret").toString())) {
                paramMap = (Map) JSONObject.toBean(JSONObject.fromObject(paramMap.get("data")), Map.class);
                map.put("code", "success");
                map.put("mes", "人脸融合成功");
                map.put("data", paramMap);
            } else {
                map.put("code", "fail");
                map.put("mes", "人脸融合失败");
            }
        } catch (Exception e) {
            map.put("code", "fail");
            map.put("mes", "人脸融合异常");
            return map;
        }
        return map;
    }
}
